﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Model;
using System.Data;
using System.Data.SqlClient;

namespace Controller
{
    public class SubBranchController : Controller.dbConnection.ConnectionClass
    {
        public string Save(SubBranch SubBranchs)
        {
            SqlCommand command = new SqlCommand();
            command.CommandType = CommandType.StoredProcedure;
            command.CommandText = "UP_InsertUpdateSubBranch";
            command.Parameters.Add("@ID", SqlDbType.VarChar).Value = SubBranchs.ID;
            command.Parameters.Add("@CODE", SqlDbType.VarChar).Value = SubBranchs.Code;
            command.Parameters.Add("@ORGID", SqlDbType.VarChar).Value = SubBranchs.OrgID;
            command.Parameters.Add("@DESCRIPTION", SqlDbType.VarChar).Value = SubBranchs.Description;
            command.Parameters.Add("@LOCATIONID", SqlDbType.VarChar).Value = SubBranchs.Location.ID;
            command.Parameters.Add("@WAREHOUSEID", SqlDbType.VarChar).Value = SubBranchs.WarehouseID;
            command.Parameters.Add("@CLASSBRANCH", SqlDbType.VarChar).Value = SubBranchs.ClassBranch;
            command.Parameters.Add("@ADDRESS", SqlDbType.VarChar).Value = SubBranchs.Address;
            command.Parameters.Add("@PHONE", SqlDbType.VarChar).Value = SubBranchs.Phone;
            command.Parameters.Add("@ISACTIVE", SqlDbType.VarChar).Value = SubBranchs.isActive;
            command.Parameters.Add("@LASTMODIFIEDBY", SqlDbType.VarChar).Value = SubBranchs.LastModifiedBy;
            command.Parameters.Add("@CREATEBY", SqlDbType.VarChar).Value = SubBranchs.CreateBy;
            command.Parameters.Add("@BusinessGroupID", SqlDbType.VarChar).Value = SubBranchs.BusinessGroupID;

            return ExecuteNonQuery(command);
        }

        public string Delete(SubBranch SubBranchs)
        {
            SqlCommand command = new SqlCommand();
            command.CommandType = CommandType.StoredProcedure;
            command.CommandText = "UP_DeleteSubBranch";
            command.Parameters.Add("@ID", SqlDbType.VarChar).Value = SubBranchs.ID;
            command.Parameters.Add("@LASTMODIFIEDBY", SqlDbType.VarChar).Value = SubBranchs.LastModifiedBy;

            return ExecuteNonQuery(command);
        }

        public IList<SubBranch> GetAll(string sSearch, int PageNumber, int Datalength, string SortCol, string SortDirection, out int TotalData, int BusinessGroupID)
        {
            SqlCommand command = new SqlCommand();
            command.CommandType = CommandType.StoredProcedure;
            command.CommandText = "SP_GetAllSubBranches";
            command.Parameters.Add("@SEARCH", SqlDbType.VarChar).Value = sSearch;
            command.Parameters.Add("@BusinessGroupID", SqlDbType.VarChar).Value = BusinessGroupID;

            DataSet dx = GetTable(command);

            List<SubBranch> SubBranchs = new List<SubBranch>();

            switch (SortDirection)
            {
                case "asc":
                    switch (SortCol)
                    {
                        case "Description":
                            dx.Tables[0].DefaultView.Sort = "Description ASC";
                            break;
                        case "Code":
                            dx.Tables[0].DefaultView.Sort = "Code ASC";
                            break;
                        case "Branch":
                            dx.Tables[0].DefaultView.Sort = "LocationID ASC";
                            break;
                        case "ClassBranch":
                            dx.Tables[0].DefaultView.Sort = "ClassBranch ASC";
                            break;
                    }
                    break;
                case "desc":
                    switch (SortCol)
                    {
                        case "Description":
                            dx.Tables[0].DefaultView.Sort = "Description DESC";
                            break;
                        case "Code":
                            dx.Tables[0].DefaultView.Sort = "Code DESC";
                            break;
                        case "Branch":
                            dx.Tables[0].DefaultView.Sort = "LocationID DESC";
                            break;
                        case "ClassBranch":
                            dx.Tables[0].DefaultView.Sort = "ClassBranch DESC";
                            break;
                    }
                    break;
            }

            DataTable dt = dx.Tables[0].DefaultView.ToTable();

            if (dt.Rows.Count > 0)
            {
                for (int i = PageNumber; i < Datalength; i++)
                {
                    if (i < dt.Rows.Count)
                    {
                        SubBranch p = new SubBranch();
                        p.ID = Convert.ToInt16(dt.Rows[i]["ID"].ToString());
                        p.Code = dt.Rows[i]["Code"].ToString();
                        p.Description = dt.Rows[i]["Description"].ToString();
                        p.Location = new LocationController().GetLocationByID(Convert.ToInt16(dt.Rows[i]["LocationID"].ToString()));
                        p.WarehouseID = dt.Rows[i]["WarehouseID"].ToString();
                        p.ClassBranch = dt.Rows[i]["ClassBranch"].ToString();
                        p.Address = dt.Rows[i]["Address"].ToString();
                        p.Phone = dt.Rows[i]["Phone"].ToString();
                        p.isActive = Convert.ToBoolean(dt.Rows[i]["isActive"].ToString());
                        p.LastModifiedBy = dt.Rows[i]["LastModifiedBy"].ToString();
                        p.LastModifiedDate = dt.Rows[i]["LastModifiedDate"] == DBNull.Value ? (DateTime?)null : Convert.ToDateTime(dt.Rows[i]["LastModifiedDate"].ToString());
                        p.CreateBy = dt.Rows[i]["CreateBy"].ToString();
                        p.CreateDate = dt.Rows[i]["CreateDate"] == DBNull.Value ? (DateTime?)null : Convert.ToDateTime(dt.Rows[i]["CreateDate"].ToString());
                        p.OrgID = dt.Rows[i]["OrgID"].ToString();
                        SubBranchs.Add(p);
                    }
                    else
                    {
                        break;
                    }

                }
            }

            TotalData = dt.Rows.Count;

            return SubBranchs.ToList();
        }


        public IList<SubBranch> GetAllSubBranchesByLocationID(string sSearch, int LocationID, int PageNumber, int Datalength, string SortCol, string SortDirection, out int TotalData)
        {
            SqlCommand command = new SqlCommand();
            command.CommandType = CommandType.StoredProcedure;
            command.CommandText = "SP_GetAllSubBranchesByLocationID";
            command.Parameters.Add("@SEARCH", SqlDbType.VarChar).Value = sSearch;
            command.Parameters.Add("@LOCATIONID", SqlDbType.VarChar).Value = LocationID;

            DataSet dx = GetTable(command);

            List<SubBranch> SubBranchs = new List<SubBranch>();

            DataTable dt = dx.Tables[0].DefaultView.ToTable();

            if (dt.Rows.Count > 0)
            {
                for (int i = PageNumber; i < Datalength; i++)
                {
                    if (i < dt.Rows.Count)
                    {
                        SubBranch p = new SubBranch();
                        p.ID = Convert.ToInt16(dt.Rows[i]["ID"].ToString());
                        p.Code = dt.Rows[i]["Code"].ToString();
                        p.Description = dt.Rows[i]["Description"].ToString();
                        p.Location = new LocationController().GetLocationByID(Convert.ToInt16(dt.Rows[i]["LocationID"].ToString()));
                        p.WarehouseID = dt.Rows[i]["WarehouseID"].ToString();
                        p.ClassBranch = dt.Rows[i]["ClassBranch"].ToString();
                        p.Address = dt.Rows[i]["Address"].ToString();
                        p.Phone = dt.Rows[i]["Phone"].ToString();
                        p.isActive = Convert.ToBoolean(dt.Rows[i]["isActive"].ToString());
                        p.LastModifiedBy = dt.Rows[i]["LastModifiedBy"].ToString();
                        p.LastModifiedDate = dt.Rows[i]["LastModifiedDate"] == DBNull.Value ? (DateTime?)null : Convert.ToDateTime(dt.Rows[i]["LastModifiedDate"].ToString());
                        p.CreateBy = dt.Rows[i]["CreateBy"].ToString();
                        p.CreateDate = dt.Rows[i]["CreateDate"] == DBNull.Value ? (DateTime?)null : Convert.ToDateTime(dt.Rows[i]["CreateDate"].ToString());
                        p.OrgID = dt.Rows[i]["OrgID"].ToString();
                        SubBranchs.Add(p);
                    }
                    else
                    {
                        break;
                    }

                }
            }

            TotalData = dt.Rows.Count;

            return SubBranchs.ToList();
        }

        public SubBranch GetSubBranchByID(int ID)
        {
            SqlCommand command = new SqlCommand();
            command.CommandType = CommandType.StoredProcedure;
            command.CommandText = "SP_GetSubBranchByID";
            command.Parameters.Add("@ID", SqlDbType.Int).Value = ID;

            IList<string> Data = GetSingleRow(command);
            SubBranch SubBranchs = null;

            if (Data != null && Data.Count > 0)
            {
                SubBranchs = new SubBranch();
                SubBranchs.ID = Convert.ToInt16(Data[0]);
                SubBranchs.Code = Data[1].ToString();
                SubBranchs.Description = Data[2].ToString();
                SubBranchs.Location = new LocationController().GetLocationByID(Convert.ToInt16(Data[3]));
                SubBranchs.WarehouseID = Data[4].ToString();
                SubBranchs.ClassBranch = Data[5].ToString();
                SubBranchs.Address = Data[6].ToString();
                SubBranchs.Phone = Data[7].ToString();
                SubBranchs.isActive = Convert.ToBoolean(Data[8]);
                SubBranchs.LastModifiedDate = Data[9] == string.Empty ? (DateTime?)null : Convert.ToDateTime(Data[9].ToString());
                SubBranchs.LastModifiedBy = Data[10].ToString();
                SubBranchs.CreateDate = Data[11] == string.Empty ? (DateTime?)null : Convert.ToDateTime(Data[11].ToString());
                SubBranchs.CreateBy = Data[12].ToString();
                SubBranchs.OrgID = Data[13].ToString();
            }

            return SubBranchs;
        }

        public SubBranch GetSubBranchByCode(string Code)
        {
            SqlCommand command = new SqlCommand();
            command.CommandType = CommandType.StoredProcedure;
            command.CommandText = "SP_GetSubBranchByCode";
            command.Parameters.Add("@Code", SqlDbType.VarChar).Value = Code;

            IList<string> Data = GetSingleRow(command);
            SubBranch SubBranchs = null;

            if (Data != null && Data.Count > 0)
            {
                SubBranchs = new SubBranch();
                SubBranchs.ID = Convert.ToInt16(Data[0]);
                SubBranchs.Code = Data[1].ToString();
                SubBranchs.Description = Data[2].ToString();
                SubBranchs.Location = new LocationController().GetLocationByID(Convert.ToInt16(Data[3]));
                SubBranchs.WarehouseID = Data[4].ToString();
                SubBranchs.ClassBranch = Data[5].ToString();
                SubBranchs.Address = Data[6].ToString();
                SubBranchs.Phone = Data[7].ToString();
                SubBranchs.isActive = Convert.ToBoolean(Data[8]);
                SubBranchs.LastModifiedDate = Data[9] == string.Empty ? (DateTime?)null : Convert.ToDateTime(Data[9].ToString());
                SubBranchs.LastModifiedBy = Data[10].ToString();
                SubBranchs.CreateDate = Data[11] == string.Empty ? (DateTime?)null : Convert.ToDateTime(Data[11].ToString());
                SubBranchs.CreateBy = Data[12].ToString();
                SubBranchs.OrgID = Data[13].ToString();
            }

            return SubBranchs;
        }


        public SubBranch GetSubBranchByDescription(string Description)
        {
            SqlCommand command = new SqlCommand();
            command.CommandType = CommandType.StoredProcedure;
            command.CommandText = "SP_GetSubBranchBy_Description";
            command.Parameters.Add("@Description", SqlDbType.VarChar).Value = Description;

            IList<string> Data = GetSingleRow(command);
            SubBranch SubBranchs = null;

            if (Data != null && Data.Count > 0)
            {
                SubBranchs = new SubBranch();
                SubBranchs.ID = Convert.ToInt16(Data[0]);
                SubBranchs.Code = Data[1].ToString();
                SubBranchs.Description = Data[2].ToString();
                SubBranchs.Location = new LocationController().GetLocationByID(Convert.ToInt16(Data[3]));
                SubBranchs.WarehouseID = Data[4].ToString();
                SubBranchs.ClassBranch = Data[5].ToString();
                SubBranchs.Address = Data[6].ToString();
                SubBranchs.Phone = Data[7].ToString();
                SubBranchs.isActive = Convert.ToBoolean(Data[8]);
                SubBranchs.LastModifiedDate = Data[9] == string.Empty ? (DateTime?)null : Convert.ToDateTime(Data[9].ToString());
                SubBranchs.LastModifiedBy = Data[10].ToString();
                SubBranchs.CreateDate = Data[11] == string.Empty ? (DateTime?)null : Convert.ToDateTime(Data[11].ToString());
                SubBranchs.CreateBy = Data[12].ToString();
                SubBranchs.OrgID = Data[13].ToString();
            }

            return SubBranchs;
        }
    }
}